package libaryinterface;

import jdbc.UpdateJdbc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-15
 * Time: 19:11
 * author:UpdateInterface
 */
public class UpdateInterface extends JFrame {
    public String id;
    public UpdateInterface( Object[][] rowdata,int index) {

        //设置窗口
        super("修改界面");
        Container container = this.getContentPane();

        //设置标签字体
        Font font = new Font("黑体", Font.BOLD, 20);

        //创建标签
        JLabel label1 = new JLabel("SNID:");
        label1.setFont(font );
        label1.setBounds(150,100,100,40);

        JLabel label2 = new JLabel("书名:");
        label2.setFont(font );
        label2.setBounds(150,150,100,40);

        JLabel label3 = new JLabel("作者:");
        label3.setFont(font );
        label3.setBounds(150,200,100,40);

        JLabel label4 = new JLabel("出版社:");
        label4.setFont(font );
        label4.setBounds(150,250,100,40);

        JLabel label5 = new JLabel("是否借出:");
        label5.setFont(font );
        label5.setBounds(150,300,100,40);

        JLabel label6 = new JLabel("借阅人:");
        label6.setFont(font );
        label6.setBounds(150,350,100,40);
        container.add(label1);
        container.add(label2);
        container.add(label3);
        container.add(label4);
        container.add(label5);
        container.add(label6);

        //创建文本框
        JTextField textField1 = new JTextField();
        //将选中行现有的值放入文本框中 , 下同
        textField1.setText((String) rowdata[index][0]);
        textField1.setBounds(250,100,300,40);
        textField1.setFont(font);

        JTextField textField2 = new JTextField();
        textField2.setText((String) rowdata[index][1]);
        textField2.setBounds(250,150,300,40);
        textField2.setFont(font);

        JTextField textField3 = new JTextField();
        textField3.setText((String) rowdata[index][2]);
        textField3.setBounds(250,200,300,40);
        textField3.setFont(font);

        JTextField textField4 = new JTextField();
        textField4.setText((String) rowdata[index][3]);
        textField4.setBounds(250,250,300,40);
        textField4.setFont(font);

        JTextField textField5 = new JTextField();
        textField5.setText((String) rowdata[index][4]);
        textField5.setBounds(250,300,300,40);
        textField5.setFont(font);

        JTextField textField6 = new JTextField();
        textField6.setText((String) rowdata[index][5]);
        textField6.setBounds(250,350,300,40);
        textField6.setFont(font);

        container.add(textField1);
        container.add(textField2);
        container.add(textField3);
        container.add(textField4);
        container.add(textField5);
        container.add(textField6);

        //创建按钮
        JButton button = new JButton("保存");
        button.setBounds(350,450,80,30);
        button.setFont(font);

        this.id = (String) rowdata[index][0];
        //设置监听
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag;
                try {
                    UpdateJdbc updateJdbc = new UpdateJdbc();
                    String snid = textField1.getText();
                    String name = textField2.getText();
                    String author = textField3.getText();
                    String press = textField4.getText();
                    String isBorrow = textField5.getText();
                    String Borrower = textField6.getText();

                    flag = updateJdbc.updateAll(id,snid,name,author,press,isBorrow,Borrower);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                if( flag) {
                    new MyDialogDemo("修改成功！");
                } else {
                    new MyDialogDemo("修改失败");
                }

            }
        });

        //获得容器

        container.setBackground(new Color(50, 144, 236));
        setBounds(200,200,800,600);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //添加
        container.add(button);
    }
}
